package com.koron.standard.jobStock.experiment.controller;


import cn.hutool.poi.excel.WorkbookUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.koron.bean.base.Response;
import com.koron.standard.bean.query.PageQuery;
import com.koron.standard.deviceClasssify.service.TypeService;
import com.koron.standard.jobStock.experiment.bean.dto.ExperimentDTO;
import com.koron.standard.jobStock.experiment.bean.query.ExperimentQuery;
import com.koron.standard.jobStock.experiment.bean.vo.ExperimentVO;
import com.koron.standard.jobStock.experiment.excel.ExperimentListener;
import com.koron.standard.jobStock.experiment.service.ExperimentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

/**
 * 试验标准信息(Experiment)表控制层
 *
 * @author jzp
 * @since 2021-09-16
 */
@Api(tags = "试验标准信息接口")
@RestController
@RequestMapping("experiment")
public class ExperimentController {

    @Autowired
    private ExperimentService experimentService;

    @Autowired
    private TypeService typeService;

    @ApiOperation(value = "分页")
    @GetMapping("page")
    public Response<Page<ExperimentVO>> page(PageQuery<ExperimentVO> pageQuery, ExperimentQuery query) {
        Page<ExperimentVO> page = experimentService.page(pageQuery, query);
        return Response.succ(page, page.getTotal());
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping
    public Response<ExperimentDTO> select(@RequestParam String id) {
        ExperimentDTO experimentDTO = experimentService.getDtoById(id);
        return Response.ok(experimentDTO);
    }

    @ApiOperation(value = "保存")
    @PostMapping
    public Response save(@RequestBody ExperimentDTO experimentDTO) {
        experimentService.saveDto(experimentDTO);
        return Response.ok(experimentDTO.getId());
    }

    @ApiOperation(value = "修改")
    @PutMapping
    public Response update(@RequestBody ExperimentDTO experimentDTO) {
        experimentService.updateDtoById(experimentDTO);
        return Response.ok();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping
    public Response delete(@RequestParam("ids") List<String> ids) {
        experimentService.removeByIds(ids);
        return Response.ok();
    }

    @ApiOperation(value = "设置状态")
    @PutMapping("updateStatus")
    public Response updateStatus(@RequestParam String id, @RequestParam Integer status) {
        experimentService.updateStatus(id, status);
        return Response.ok();
    }

    @ApiOperation(value = "设置为公共标准")
    @PutMapping("updateToPublic")
    public Response updateToPublic(@RequestParam("ids") List<String> ids) {
        experimentService.updateToPublic(ids);
        return Response.ok();
    }

    @SneakyThrows
    @ApiOperation(value = "下载模板")
    @GetMapping("template")
    public void template(HttpServletResponse response) {
        String path = "excel/job_experiment_standard.xlsx";
        ClassPathResource classPathResource = new ClassPathResource(path);
        Workbook book = WorkbookUtil.createBook(classPathResource.getInputStream());
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("试验标准", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        book.write(response.getOutputStream());
    }

    @SneakyThrows
    @ApiOperation(value = "导入")
    @PostMapping("import")
    public Response importFile(@RequestParam("file") MultipartFile file) {
        ExperimentListener experimentListener = new ExperimentListener(typeService, experimentService);
        EasyExcel.read(file.getInputStream(), experimentListener).doReadAll();
        return experimentListener.getSheetResponse();
    }

}
